import type { RequestConfig } from 'umi';
import { persistStore, persistReducer } from 'redux-persist';
import { Local } from './utils/storage';
import storage from 'redux-persist/lib/storage';
import { Reducer, Action } from 'redux';
import { PersistPartial } from 'redux-persist/es/persistReducer';

const persistConfig = {
  key: `${APP_NAME}`,
  storage,
};

const persistEnhancer =
  () =>
  (createStore: (arg0: Reducer<PersistPartial, Action<any>>, arg1: any, arg2: any) => any) =>
  (reducer: Reducer<unknown, Action<any>>, initialState: any, enhancer: any) => {
    const store = createStore(persistReducer(persistConfig, reducer), initialState, enhancer);
    const persist = persistStore(store, null);
    return {
      persist,
      ...store,
    };
  };

export const dva = {
  config: {
    extraEnhancers: [persistEnhancer()],
  },
};

export const request: RequestConfig = {
  timeout: 60000,
  errorConfig: {
    adaptor: (resData) => {
      return {
        ...resData,
        // success: resData.rt === 1 ? true : false,
        success: true,
        errorCode: resData.error,
        errorMessage: resData.data.message,
        showType: 4,
      };
    },
  },
  middlewares: [],
  requestInterceptors: [
    (url: any, options: any) => {
      const token = Local?.get('token') || '';
      if (options.requestType === 'form') {
        options.data.append('token', token);
      } else {
        options.data = {
          ...options.data,
          ...{
            token,
          },
        };
      }
      return {
        url,
        options,
      };
    },
  ],
  responseInterceptors: [],
};
